package com.koron.css2.contractmanage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.impl.AsyncSendMsg;
import com.koron.css2.contractmanage.Enum.ContractStatusEnum;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.bean.ContractManageBean;
import com.koron.css2.contractmanage.exception.ContractException;
import com.koron.css2.contractmanage.mapper.ContractManageMapper;
import com.koron.css2.contractmanage.service.EcContratServiceImpl;
import com.koron.css2.contractmanage.util.EcConstant;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.SpringBeanUtil;
import net.qiyuesuo.v3sdk.utils.SdkResponse;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class ContractNotify implements ServerInterface {

    private EcContratServiceImpl ecContratService = new EcContratServiceImpl();

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ContractManageMapper mapper = factory.getMapper(ContractManageMapper.class);
            ContractManageBean bean = JsonUtils.objectToPojo(req.getData(), ContractManageBean.class);
            if (bean == null) {
                throw new ContractException("合同催签请求参数不能为空！");
            }

            // 合同状态
            String contractType = bean.getContractType();
            //校验合同字段
            checkContractManageBean(bean, userInfo);

            // 电子施工合同，调接口发送催签消息
            if (ContractTypeEnum.CONTRACT_TYPE_2.equals(contractType)) {
                String recordId = getRecordId(bean, mapper);
                SdkResponse responseObj = ecContratService.ecContractNotify(Long.valueOf(recordId));
                logger.debug(bean.getUserNo() + "合同催签,responseObj:" + JsonUtils.objectToJson(responseObj));
                if (EcConstant.MESSAGE_SUCCESS != responseObj.getCode()) {
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "合同催签失败！" + responseObj.getCode() + ":" + responseObj.getMessage() , null);
                }
            } else if (ContractTypeEnum.CONTRACT_TYPE_3.equals(contractType)) { // 普通施工合同，发送微信模板消息
                Map<String, String> phraseFieldData = new HashMap<>();
                phraseFieldData.put("userNo", bean.getUserNo());
                phraseFieldData.put("contractNo", bean.getContractNo());
                phraseFieldData.put("contractTypeName", ContractTypeEnum.getContractTypeName(bean.getContractType()));
                phraseFieldData.put("contractStatusName", ContractStatusEnum.getContractStatusName(bean.getContractStatus()));
                MessageBean msgBean = SpringBeanUtil.getBean(AsyncSendMsg.class).sendWxMessageToUserContract(
                        16, phraseFieldData, userInfo.getCurWaterCode(), userInfo.getUserInfo().getCode());
                if (Constant.MESSAGE_INT_SUCCESS != msgBean.getCode()) {
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "合同催签，发送微信消息失败：" + JsonUtils.objectToJson(msgBean), null);
                }
            }

            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "催签成功！", void.class);
        } catch (Exception e) {
            logger.error("合同催签失败: " + e.getMessage(), e);
            e.printStackTrace();
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "合同催签失败: " + e.getMessage(), null);
        }
    }

    /**
     * 校验合同字段
     *
     * @return
     */
    public void checkContractManageBean(ContractManageBean bean, UserInfoBean userInfo) {
        if (userInfo == null) {
            throw new ContractException("用户信息不能为空！");
        }
        if (StringUtils.isBlank(userInfo.getCurWaterCode())) {
            throw new ContractException("用户信息中的水司编号不能为空！");
        }
        if (StringUtils.isBlank(bean.getUserNo())) {
            throw new ContractException("参数中的户号不能为空！");
        }
        if (StringUtils.isBlank(bean.getContractNo())) {
            throw new ContractException("参数中的合同编码不能为空！");
        }
        if (StringUtils.isBlank(bean.getContractType())) {
            throw new ContractException("参数中的合同类型不能为空！");
        }
        if (StringUtils.isBlank(bean.getContractStatus())) {
            throw new ContractException("参数中的合同状态不能为空！");
        }

        if (!ContractStatusEnum.CONTRACT_STATUS_1.getContractStatus().equals(bean.getContractStatus())) {
            throw new ContractException("只有进行中的合同才需要催签！");
        }
    }

    /**
     *
     * @param bean
     * @param mapper
     * @return
     */
    public String getRecordId(ContractManageBean bean, ContractManageMapper mapper) {
        ContractManageBean contractManageBean = mapper.getContractDetail(bean.getUserNo(), bean.getContractNo());
        if (contractManageBean == null ) {
            throw new ContractException("根据户号" + bean.getUserNo() + "，合同编号" + bean.getContractNo() + "找不到合同！");
        }
        return contractManageBean.getRecordId();
    }
}
